$(function () {
    //加载layui
    layui.use('element', function () {
        var element = layui.element;
    });
    //点击侧边栏切换视图

    $(".change-view").click(function () {
        $("#iframe_view").attr("src", $(this).data("url"));
    })
})

//渲染侧边栏
$(function () {
    $.get("/menu/queryRootMenu", function (data) {
        console.log(data);
        if (data.length != 0) {
            for (var i = 0; i < data.length; i++) {
                var root =
                    $('<li data-name="set" class="layui-nav-item">' +
                        '<a href="javascript:;" lay-tips=' + data[i].text + ' lay-direction="2">' +
                        '<i class="layui-icon layui-icon-' + data[i].icon + '"></i>' +
                        '<cite id="cite">' + data[i].text + '</cite>' +
                        '</a>' +
                        '</li>');
                createTree(data[i], root);
                $("#LAY-system-side-menu").append(root);
                layui.use('element', function () {
                    var element = layui.element;
                    element.init();
                });
            }
        }
    });

    var createTree = function (data, parent) {
        if (data.children.length != 0 && parent) {
            var dl = $('<dl class="layui-nav-child"></dl>');
            for (var i = 0; i < data.children.length; i++) {
                if (data.children[i].children.length == 0) {
                    var dd = $(' <dd data-name="button">\n' +
                        '            <a lay-href=' + data.children[i].url + '>' + data.children[i].text + '</a>\n' +
                        '            </dd>');
                } else {
                    var dd = $(' <dd data-name="button">\n' +
                        '            <a href="javascript:;">' + data.children[i].text + '</a>\n' +
                        '            </dd>');
                }
                dl.append(dd);
                createTree(data.children[i], dd);
                parent.append(dl);
            }
        }
    };


    $("#logout_btn").click(function () {
        $.get("/logout", function (data) {
            window.parent.location.href = "/";
        })
    })

})

//全屏
$(function () {
    $('#admin-side-full').on('click', function () {
        var docElm = document.documentElement;
        //W3C
        if (docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }
        //FireFox
        else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }
        //Chrome等
        else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
        //IE11
        else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
        layer.msg('按Esc即可退出全屏');
    });
})


$(function () {
    //锁屏
    $(document).on('keydown', function () {
        var e = window.event;
        if (e.keyCode === 76 && e.altKey) {
            //alert("你按下了alt+l");
            lock($, layer);
        }
    });
    $('#lock').on('click', function () {
        lock($, layer);
    });
})

var isShowLock = false;

function lock($, layer) {
    if (isShowLock)
        return;
    //自定页
    layer.open({
        title: false,
        type: 1,
        closeBtn: 0,
        anim: 6,
        content: $('#lock-temp').html(),
        shade: [0.9, '#393D49'],
        success: function (layero, lockIndex) {
            isShowLock = true;
            //给显示用户名赋值
            //layero.find('div#lockUserName').text('admin');
            //layero.find('input[name=username]').val('admin');
            layero.find('input[name=password]').on('focus', function () {
                var $this = $(this);
                if ($this.val() === '输入密码解锁..') {
                    $this.val('').attr('type', 'password');
                }
            })
                .on('blur', function () {
                    var $this = $(this);
                    if ($this.val() === '' || $this.length === 0) {
                        $this.attr('type', 'text').val('输入密码解锁..');
                    }
                });

            //绑定解锁按钮的点击事件
            layero.find('button#unlock').on('click', function () {
                var $lockBox = $('div#lock-box');
                var pwd = $lockBox.find('input[name=lockPwd]').val();
                if (pwd.trim().length === 0) {
                    layer.msg('请输入密码..', {
                        icon: 2,
                        time: 1000
                    });
                    return;
                }
                unlock(pwd);
            });
            /**
             * 解锁操作方法
             * @param {String} 密码
             */
            var unlock = function (pwd) {
                //这里可以使用ajax方法解锁
                $.post('/staff/unlock', {password: pwd}, function (res) {
                    console.log(res)
                    //验证成功
                    if (res.success) {
                        //关闭锁屏层
                        layer.close(lockIndex);
                        isShowLock = false;
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }
                }, 'json');
            };
        }
    });
};

function signinTime() {
    $.ajax({
        type: "POST",
        url: "/signin/signinTime",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.success) {
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        }
    })
}

function signoutTime() {
    $.ajax({
        type: "POST",
        url: "/signin/signoutTime",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.success) {
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        }
    })

}
//视频播放
$(function () {
    $('#video').on('click', function () {
        layer.open({
            title: '扣丁狼',
            maxmin: true,
            type: 2,
            content: 'https://ke.qq.com/webcourse/index.html#course_id=272077&term_id=100321361&taid=1875217081444045&vid=s1423xg0yo0',
            area: ['800px', '500px']
        });
    });
})